#include "stdio.h"
#include "stm32f10x.h"

int main() {}

void USART1_Init() {
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    // TX
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    // RX
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = 9600;  // 波特率
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;  // 数据位
    USART_InitStructure.USART_StopBits = USART_StopBits_1;  // 停止位
    USART_InitStructure.USART_Parity = USART_Parity_No;  // 校验位
    USART_InitStructure.USART_Mode = USART_Mode_Tx;  // 发送模式
    USART_InitStructure.USART_HardwareFlowControl =
        USART_HardwareFlowControl_None;

    USART_Init(USART1, &USART_InitStructure);

    // 开启
    USART_Cmd(USART1, ENABLE);
}

int fputc(int ch, FILE* f) {
    if (ch == '\n') {
        while (!USART_GetFlagStatus(USART1, USART_FLAG_TC))
            ;
        USART_SendData(USART1, '\r');
    }
    while (!USART_GetFlagStatus(USART1, USART_FLAG_TC))
        ;
    USART_SendData(USART1, ch);

    return ch;
}